import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';
import {User} from '../providers/user';
import 'rxjs/add/operator/map';
import {Events} from 'ionic-angular';
import {Machine} from "./machine";

@Injectable()
export class AppData {

  constructor(public events: Events, private http: Http) {

  }

  login(user: User) {

    return new Promise(resolve => {
      let headers = new Headers();
      headers.append('Content-Type', 'application/json');
      this.http.post('http://localhost:8080/login', user, {headers: headers})
        .subscribe((res) => {
          let status = res.json().result;
          this.events.publish('login:' + status);
          resolve(res);
        });
    });
  }

  saveUser(user: User) {
    return new Promise(resolve => {
      let headers = new Headers();
      headers.append('Content-Type', 'application/json');
      this.http.post('http://localhost:8080/admin/user/save', user, {headers: headers})
        .subscribe((res) => {
          // let status = res.json().result;
          // this.events.publish('saveUser:' + status);
          resolve(res);
        });
    });
  }

  getUserList() {
    return new Promise(resolve => {
      let headers = new Headers();
      // headers.append('Content-Type', 'application/json');
      this.http.post('http://localhost:8080/admin/user/getUserList', "", {headers: headers})
        .subscribe((res) => {
          resolve(res);
        });
    });
  }

  deleteUser(user){
    return new Promise(resolve => {
      let headers = new Headers();
      headers.append('Content-Type', 'application/json');
      this.http.post('http://localhost:8080/admin/user/deleteUser', user, {headers: headers})
        .subscribe((res) => {
          resolve(res);
        });
    });
  }

  saveMachine(machine: Machine) {
    return new Promise(resolve => {
      let headers = new Headers();
      headers.append('Content-Type', 'application/json');
      this.http.post('http://localhost:8080/admin/machine/save', machine, {headers: headers})
        .subscribe((res) => {
          let status = res.json().result;
          this.events.publish('saveMachine:' + status);
          resolve(res);
        });
    });
  }

  getMachineList() {
    return new Promise(resolve => {
      let headers = new Headers();
      // headers.append('Content-Type', 'application/json');
      this.http.post('http://localhost:8080/admin/machine/getMachineList', "", {headers: headers})
        .subscribe((res) => {
          resolve(res);
        });
    });
  }

  deleteMachine(machine){
    return new Promise(resolve => {
      let headers = new Headers();
      headers.append('Content-Type', 'application/json');
      this.http.post('http://localhost:8080/admin/machine/deleteMachine', machine, {headers: headers})
        .subscribe((res) => {
          resolve(res);
        });
    });
  }

  getOnlineMachineList() {
    return new Promise(resolve => {
      let headers = new Headers();
      // headers.append('Content-Type', 'application/json');
      this.http.post('http://localhost:8080/admin/machine/getOnlineMachineList', "", {headers: headers})
        .subscribe((res) => {
          resolve(res);
        });
    });
  }

}
